package com.woniu.user.controller;

import com.woniu.common.annotation.LoginUser;
import com.woniu.common.entity.Result;
import com.woniu.user.dto.AddressDTO;
import com.woniu.user.pojo.Address;
import com.woniu.user.service.IAddressService;
import com.woniu.user.vo.AddressVO;
import com.woniu.user.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址表 前端控制器
 * </p>
 *
 * @author hushunlai
 * @since 2023-05-22
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地址管理")
public class AddressController {

    @Autowired
    private IAddressService addressService;

    @GetMapping("/listAddress")
    @ApiOperation("查询该用户的所有地址")
    public Result listAddress(@LoginUser UserLoginVO userLoginVO) {

        List<AddressVO> addressVOList = addressService.listAddress(userLoginVO);
        return Result.ok(addressVOList);
    }

    @PostMapping("/addAddress")
    @ApiOperation("增加地址")
    public Result addAddress(@RequestBody AddressDTO addressDTO, @LoginUser UserLoginVO userLoginVO) {

        Boolean flag = addressService.addAddress(addressDTO, userLoginVO);
        return Result.ok(flag);
    }

    @PostMapping("/updateAddress")
    @ApiOperation("修改地址")
    public Result updateAddress(@RequestBody AddressDTO addressDTO, @LoginUser UserLoginVO userLoginVO) {

        Boolean flag = addressService.updateAddress(addressDTO, userLoginVO);
        return Result.ok(flag);
    }

    @PostMapping("/deleteAddress")
    @ApiOperation("删除地址")
    public Result deleteAddress(@RequestBody AddressDTO addressDTO, @LoginUser UserLoginVO userLoginVO) {

        Boolean flag = addressService.deleteAddress(addressDTO, userLoginVO);
        return Result.ok(flag);
    }

    @GetMapping("/getAddress/{addressId}/{mobile}")
    @ApiOperation("根据地址id查询某个地址 linchuan")
    public Result<Address> getAddress(@PathVariable("addressId") Integer addressId,
                                      @PathVariable("mobile") String mobile) {

        Address address = addressService.getAddress(addressId, mobile);
        return Result.ok(address);
    }

    @GetMapping("/getDefaultAddress")
    @ApiOperation("查询默认地址")
    public Result<AddressVO> getDefaultAddress(@LoginUser UserLoginVO userLoginVO){
        AddressVO addressVO = addressService.getDefaultAddress(userLoginVO.getMobile());
        return Result.ok(addressVO);
    }



    @GetMapping("/{addressId}")
    @ApiOperation("根据地址id查询某个地址")
    public Result<Address> getById(@PathVariable("addressId") Integer addressId) {
        Address address = addressService.getById(addressId);
        return Result.ok(address);
    }
}
